<template>
  <div class="wrapper">

    <!-- header部分 -->
    <header>
      <i class="fa fa-angle-left" @click="goback"></i>
      <p>在线支付</p>
    </header>

    <!-- 订单信息部分 -->
    <h3>订单信息：</h3>
    <div class="order-info">
      <p>
        {{this.orders.business.businessName}}
        <i class="fa fa-caret-down" @click="detailetShow"></i>
      </p>
      <p>&#165;{{this.orders.orderTotal}}</p>
    </div>

    <!-- 订单明细部分 -->
    <ul class="order-detailet" v-show="isShowDetailet">
      <li v-for="item in orders.list">
        <p>{{item.foodName}} x {{item.quantity}}</p>
        <p>&#165;{{(item.foodPrice*item.quantity).toFixed(2)}}</p>
      </li>
      <li>
        <p>配送费</p>
        <p>&#165;{{this.orders.business.deliveryPrice}}</p>
      </li>
    </ul>

    <!-- 使用余额部分 -->
    <ul class="payment-money">
      <li  @click="useMoney(true)">
        <div class="money">
          <p>{{ this.virtualWallet.money < (this.orders.orderTotal / 10) ? '无可使用余额' : '可抵扣 ' + parseInt(this.orders.orderTotal / 10) + ' 元' }}</p>
          <i class="fa fa-check-circle" v-show="this.use_money"></i>
        </div>
      </li>
    </ul>

    <!-- 显示实际付款部分 -->
    <div class="pay">
      <p class="p1">实际需付</p>
      <!-- <p class="p3">{{Allprice}}</p> -->
      <p class="p2">&#165;{{ this.use_money && (this.virtualWallet.money >= (this.orders.orderTotal / 10)) ? (this.orders.orderTotal - parseInt(this.orders.orderTotal / 10).toFixed(2) + this.orders.business.deliveryPrice ) : (this.orders.orderTotal + this.orders.business.deliveryPrice).toFixed(2)  }}</p>

    </div>

    <!-- 支付方式部分 -->
    <ul class="payment-type">
      <li  @click="alipay(true)">
        <img src="../assets/alipay.png">
        <p>
          支付宝
          <i class="fa fa-check-circle" v-show="isalipay"></i>
        </p>
      </li>
      <li  @click="alipay(false)">
        <img src="../assets/wechat.png">
        <p>
          微信支付
          <i class="fa fa-check-circle" v-show="!isalipay"></i>
        </p>
      </li>
    </ul>




    <div class="payment-button">
      <button @click="makePayment"  >确认支付</button>
    </div>




    <!-- 底部菜单部分 -->
    <Footer></Footer>
  </div>

</template>

<script>
import Footer from '../components/Footer.vue';
import virtualWallet from "@/views/VirtualWallet.vue";
import orders from "@/views/Orders.vue";


export default {
  name: 'Payment',
  data(){
    return{
      orderId:this.$route.query.orderId,
      orders:{
        business:{}
      },
      EffectiveIntegral:0,
      isShowDetailet:false,
      isalipay:true,
      virtualWallet: {},
      use_money:true
    }
  },
  created(){

    let url1 = `OrdersController/getOrdersById/${this.orderId}`;
    this.user = this.$getSessionStorage('user');
    this.$axios.get(url1,{
      headers : {
        'token': sessionStorage.getItem('token')
      }
    }).then(response => {
      this.orders = response.data.result;
    }).catch(error => {
      console.error(error);
    });

    //获取我的余额数据
    let url2 = `WalletController/getWalletByUserid/${this.user.userId}`;
    this.$axios.get(url2,{
      headers : {
        'token': sessionStorage.getItem('token')
      }
    }).then(response => {
      this.virtualWallet = response.data.result;
      console.log(this.virtualWallet.money);
    }).catch(error => {
      console.error(error);
    });

    //从积分明细表获取当前有效积分
    let url3 = `PointController/getPointLogByUserIdByTime/${this.user.userId}`;
    this.$axios.get(url3,{
      headers : {
        'token': sessionStorage.getItem('token')
      }
    }).then(response => {
      this.EffectiveIntegral = response.data.result;
    }).catch(error => {
      console.error(error);
    });

  },
  computed: {
    Allprice() {
      let LPrice = this.use_money && (this.virtualWallet.money >= (this.orders.orderTotal / 10)) ? (this.orders.orderTotal - parseInt(this.orders.orderTotal / 10).toFixed(2)) : this.orders.orderTotal;
      return LPrice + this.orders.business.deliveryPrice;

    }

  },



  mounted(){
    //这里的代码是实现：一旦路由到在线支付组件就不能回到订单确认组件
    //先将当前路由添加到history对象中
    history.pushState(null,null,document.URL);
    //popstate事件能够监听history对象的变化
    window.onpopstate = () => {
      this.$router.push({path:'/index'});
    }
  },

  destroyed(){
    window.onpopstate = null;
  },


  methods:{
    goback() {
      this.$router.go(-1);
    },
    detailetShow(){
      this.isShowDetailet = !this.isShowDetailet;
    },
    alipay(n){
      this.isalipay = n;
    },
    useMoney(n){
      this.use_money = !this.use_money;
    },


    makePayment() {
      let url1 = `OrdersController/turnOrderState/${this.orderId}`;
      this.$axios.put(url1,{},{
        headers : {
          'token': sessionStorage.getItem('token')
        }
      }).then(response => {
        if (response.data.result > 0) {
          //余额使用
          if(this.use_money && (this.virtualWallet.money >= (this.orders.orderTotal / 10))){
            let url2 = `WalletController/updateMoney/${this.user.userId}/${this.virtualWallet.money - parseInt(this.orders.orderTotal / 10)}`;
            this.$axios.put(url2).then(response => {
              if (response.data.result > 0) {
                alert('抵扣金额成功！');
              } else {
                alert('抵扣金额失败！');
              }
            })
                .catch(error => {
                  console.error(error);
                });
          }
          //支付订单后增加积分*************************************************
          //单个积分明细表 增加count条
          let url3 = `PointController/savePointLogByPay/${this.user.userId}/${parseInt(this.orders.orderTotal/10)}`;
          this.$axios.post(url3,{},{
            headers : {
              'token': sessionStorage.getItem('token')
            }
          }).then(response => {
            if (response.data.result > 0) {
              alert('恭喜！'+ parseInt(this.orders.orderTotal/10) +'积分已溜进你的口袋!');


              let url4 = `PointController/savePointTrans/${this.user.userId}/${1}/${parseInt(this.orders.orderTotal/10)}`;
              this.$axios.post(url4).then(response => {
                if(response.data.result>0){
                  // alert('积分明细表更新成功！')
                }else{
                  alert('积分明细表更新失败！')
                }
              }).catch(error => {
                console.error(error);
              });
            }else {
              alert('积分增加失败！');
            }
          }).catch(error => {
            console.error(error);
          });

          alert('支付成功！');

          this.$router.push({path: '/orderList'});
        } else {
          alert('支付失败！');
        }
      }).catch(error => {
        console.error(error);
      });

      // 在支付成功后，你可以执行其他的操作，比如更新页面内容或跳转到其他页面
    }
  },


  components: {
    Footer
  }
}
</script>

<style scoped>
/********** 总容器 **********/
.wrapper {
  width: 100%;
  height: 100%;
}

/********** header **********/
.wrapper header {
  width: 100%;
  height: 12vw;
  background: linear-gradient(#F37373, #F08080, #EF9292);
  color: #fff;
  font-size: 4.8vw;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
  display: flex;
  align-items: center;

}

.wrapper header i {
  margin-left: 6vw;
}

.wrapper header p {
  margin-left: 33vw;
}

/********** 订单信息部分 ***********/
.wrapper h3 {
  margin-top: 12vw;
  box-sizing: border-box;
  padding: 4vw 4vw 0;

  font-size: 4vw;
  font-weight: 300;
  color: #999;
}

.wrapper .order-info {
  box-sizing: border-box;
  padding: 4vw;
  font-size: 4vw;
  color: #666;

  display: flex;
  justify-content: space-between;
  align-items: center;
}

.wrapper .order-info p:last-child {
  color: #666;
}

/********** 订单明细部分 **********/
.wrapper .order-detailet {
  width: 100%;
}

.wrapper .order-detailet li {
  width: 100%;
  box-sizing: border-box;
  padding: 1vw 4vw;

  display: flex;
  justify-content: space-between;
  align-items: center;
}

.wrapper .order-detailet li p {
  font-size: 3vw;
  color: #666;
}

/********** 余额使用部分 **********/
.wrapper .payment-money {
  width: 100%;
  box-sizing: border-box;
  padding: 1vw 0;
}

.wrapper .payment-money li {
  width: 100%;
  box-sizing: border-box;
  padding: 4vw;

  display: flex;
  justify-content: space-between;
  align-items: center;
}

.wrapper .payment-money li .money{
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.wrapper .payment-money li .money p {
  width: 100%;
  box-sizing: border-box;
  font-size: 4.5vw;
  color: #666;
}

.wrapper .payment-money li .money .fa-check-circle {
  font-size: 5vw;
  color: #F08080;
  padding-left: 60vw;
}

/********** 实际需付部分 **********/
.wrapper .pay{
  display: flex;
  align-items: center;
}

.wrapper .p1{
  width: 100%;
  box-sizing: border-box;
  font-size: 4.5vw;
  color: #666;
  margin-left: 4.2vw;
}

.wrapper .p2{
  width: 100%;
  box-sizing: border-box;
  font-size: 4.5vw;
  color: orangered;
  padding-left:50vw;
}
/********** 支付方式部分 **********/
.wrapper .payment-type {
  width: 100%;
  box-sizing: border-box;
  padding: 1vw 0;
}

.wrapper .payment-type li {
  width: 100%;
  box-sizing: border-box;
  padding: 4vw;

  display: flex;
  justify-content: space-between;
  align-items: center;
}

.wrapper .payment-type li img {
  width: 8.1vw;
  height: 8.1vw;
}

.wrapper .payment-type li p {
  width: 100%;
  box-sizing: border-box;
  padding: 4.5vw;
  font-size: 4.5vw;
  color: #666;

  display: flex;
  justify-content: space-between;
  align-items: center;
}

.wrapper .payment-type li p .fa-check-circle {
  font-size: 5vw;
  color: #41A7F5;
}

.wrapper .payment-button {
  width: 100%;
  box-sizing: border-box;
  padding: 4vw;
}

.wrapper .payment-button button {
  width: 100%;
  height: 10vw;
  border: none;
  outline: none;
  border-radius: 4px;
  background-color: #41A7F5;
  color: #FFFFFF;
}
</style>
